<!DOCTYPE html>
<html class="x-admin-sm" lang="en" xmlns:th="https://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>权限管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" th:href="@{/assets/admin/css/font.css}">
    <link rel="stylesheet" th:href="@{/assets/admin/css/xadmin.css}">
    <script th:src="@{/assets/admin/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/assets/admin/js/xadmin.js}"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a href="">系统设置</a>
                <a>
                        <cite>角色管理</cite></a>
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form" action="">
                        <div class="layui-input-inline layui-show-xs-block">
                            <input class="layui-input" placeholder="角色名称" id="vo_roleName" name="vo_roleName"></div>
                        <div class="layui-input-inline layui-show-xs-block">
                            <button class="layui-btn layui-btn-warm" lay-submit lay-filter="sreach"><i
                                    class="layui-icon">&#xe615;</i>查询
                            </button>
                        </div>
                    </form>
                    <hr>
                </div>
                <div class="layui-card-header">
                    <button class="layui-btn" onclick="addRole()">
                        <i class="layui-icon">&#xe624;</i>新增角色
                    </button>
                    <button class="layui-btn layui-btn-danger" onclick="delAll()">
                        <i class="layui-icon"></i>批量删除
                    </button>
                </div>
                <div class="layui-card-body ">
                    <table class="layui-table layui-form" id="table1" lay-filter="test">
                        <thead>
                        </thead>
                    </table>
                </div>
                <div class="layui-card-body ">
                    <div class="page" id="page"></div>
                </div>
            </div>
        </div>
    </div>
</div>
<script th:src="@{/assets/admin/js/jquery.min.js}"></script>
<script th:replace="systemCommon/HarinCommon :: commonJs"></script>

<script type="text/html" id="barDemo">
    <button  class="layui-btn " style="height: 22px;line-height: 22px;padding: 0 5px;" lay-event="edit">编辑</button >
    <button  class="layui-btn layui-btn-warm" style="height: 22px;line-height: 22px;padding: 0 5px;"lay-event="configMenuPermission">菜单权限</button >
    <button  class="layui-btn layui-btn-warm" style="height: 22px;line-height: 22px;padding: 0 5px;"lay-event="configOperatePermission">操作权限</button >
    <button  class="layui-btn layui-btn-normal" style="height: 22px;line-height: 22px;padding: 0 5px;"lay-event="defaultRole">设置默认角色</button >
    <button  class="layui-btn layui-btn-danger" style="height: 22px;line-height: 22px;padding: 0 5px;" lay-event="del">删除</button >
</script>

</body>
</html>

<script>
    var table1 = null;
    $(function () {
        layui.use(['table', 'laydate', 'form', 'jquery'], function () {
            form = layui.form;
            //page = layui.page;
            table = layui.table;
            laydate = layui.table;

            //监听提交
            form.on('submit(sreach)', function (data) {
                table1.reload({
                    where: {     //请求参数
                        "roleName": data.field.vo_roleName
                    }
                });
                return false;
            });

            //第一个实例
            table1 = table.render({
                elem: '#table1',
                url: '/role/getRoleList', //数据接口
                method: "get",  //请求方式
                height: "full-100",
                where: {     //请求参数
                    "roleName": $("#vo_roleName").val()
                },
                parseData: function (res) { //res 即为原始返回的数据
                    var data = res.result;
                    //console.log(data.records);
                    //console.log(typeof data.records);
                    return {
                        "code": res.code, //解析接口状态
                        // "msg": res.message, //解析提示文本
                        "count": data.total, //解析数据长度
                        "data": data.records //解析数据列表
                    };
                },
                response: {//重新定义规则
                    //statusName: 'status', //规定数据状态的字段名称，默认：code
                    statusCode: 200, //规定成功的状态码，默认：0
                    //msgName: 'hint', //规定状态信息的字段名称，默认：msg
                    //countName: 'total', //规定数据总数的字段名称，默认：count
                    //dataName: 'rows' //规定数据列表的字段名称，默认：data
                },
                page: true, //开启分页
                limits: [10, 20, 50],//可选条目数
                toolbar: true,//若需要“列显示隐藏”、“导出”、“打印”等功能，则必须开启该参数
                loading: true, //家在动画
                cols: [
                    [ //表头
                        {type: 'checkbox', fixed: 'left'},
                        {field: 'id', title: 'ID', sort: true, fixed: 'left', width: 50, unresize: true},
                        {field: 'roleName', title: '角色名称', width: 120},
                        {field: 'remarks', title: '备注', width: 120},
                        {field: 'defaultRole', title: '作为默认角色', width: 120,templet:function(d){
                                if(d.defaultRole == true){
                                    return "是";
                                }else{
                                    return "否";
                                }
                            }},
                        {field: 'createTime', title: '创建时间', width: 165, unresize: true,templet : "<div>{{layui.util.toDateString(d.createTime, 'yyyy年MM月dd日 HH:mm:ss')}}</div>"},
                        {field: 'updateTime', title: '更新时间', width: 165, unresize: true,templet : "<div>{{layui.util.toDateString(d.updateTime, 'yyyy年MM月dd日 HH:mm:ss')}}</div>"},
                        {title: '操作', toolbar: "#barDemo",fixed:'right'}
                    ]
                ],
                done: function(res, curr, count){
                    //如果是异步请求数据方式，res即为你接口返回的信息。
                    //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                    //console.log(res);
                    res.data.forEach((item,index)=>{
                        if(item.defaultRole == false){
                            item.defaultRole = ""
                        }else if(item.defaultRole == true){
                            item.defaultRole = "是"
                        }
                    });
                }
            });

            //监听行工具事件  这个test是指，<table> 属性lay-filter = 'test'
            table.on('tool(test)', function (obj) {
                //当前行的数据
                let data = obj.data;
                //console.log(obj)
                //判断事件类型
                if (obj.event === 'del') {
                    layer.confirm('确定删除？', function (oIndex) {
                        //obj.del();
                        $.ajax({
                            url: "/role/deleteRoleById",
                            type: "delete",
                            data: {id: data.id},
                            dataType: "json",
                            success: function (data) {
                                refreshData();
                                if (data.code == 200) {
                                    layer.alert(data.message, {icon: 6}, function (index) {
                                        layer.close(index);
                                    });
                                } else {
                                    layer.alert(data.message, {icon: 5}, function (index) {
                                        layer.close(index);
                                    });
                                }
                            }
                        });
                        layer.close(oIndex);
                    });
                } else if (obj.event === 'edit') {
                    //打开编辑框
                    openLayer('修改角色信息', '/system/roleEditUI?id=' + data.id, 400, 280, false, true);
                } else if (obj.event === 'configMenuPermission') {
                    openLayer('修改角色菜单权限', '/system/menuPermission?roleId=' + data.id, 400, 550, false, false);
                } else if(obj.event === 'configOperatePermission'){
                    openLayer('修改角色操作权限', '/system/operatePermission?roleId=' + data.id, 400, 550, false, false);
                }else if(obj.event === 'defaultRole'){
                    layer.open({
                        title:"修改默认角色",
                        content: "确定修改 [\""+data.roleName+"\"] 为默认角色嘛？",
                        btn: ['确认', '取消'],
                        yes: function(index, layero){
                            //按钮【按钮一】的回调
                            //console.log("确认");
                            $.ajax({
                                url:"/role/changeDefaultRole",
                                type:"post",
                                data:{
                                    "roleId":data.id
                                },
                                success:function(data){
                                    if (data.code == 200) {
                                        layer.alert(data.message, {icon: 6}, function (iIndex) {
                                            layer.close(iIndex);
                                            refreshData();
                                        });
                                    } else {
                                        layer.alert(data.message, {icon: 5}, function (iIndex) {
                                            layer.close(iIndex);
                                            refreshData();
                                        });
                                    }
                                },
                                complete:function(XMLHttpRequest, textStatus){
                                    layer.close(index);
                                }
                            })

                        },btn2: function(index, layero){
                            //按钮【按钮二】的回调
                            //console.log("取消");
                            //return false 开启该代码可禁止点击该按钮关闭
                        }
                    });
                }
            });

        });


    });
    function addRole(){
        Harin.openLoyer1('新增角色','/system/roleEditUI',400,280,false,refreshData);
    }
    /**
     *
     *调用打开弹窗的方法
     * title：弹窗标题
     * url:访问的url
     * w:弹窗宽
     * h:弹窗高
     * full:boolean 是否全屏显示
     * backFun :关闭回调函数
     */
    function openLayer(title, url, w, h, full, backFun) {
        if (backFun == null || !backFun) {
            Harin.openLoyer1(title, url, w, h, full);
        } else {
            //调用自己封装的方法。
            Harin.openLoyer1(title, url, w, h, full, refreshData);
        }
    }

    function refreshData() {
        table1.reload({
            where: {     //请求参数
                "roleName": $("#vo_roleName").val()
                // "s_date": "2020-12-24"
            }
        });
    }
</script>
